// pages/article-detail/article-detail.js
var util = require("../../utils/util.js");
const innerAudioContext = wx.createInnerAudioContext();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        audiosrc: '',
        isPlayAudio: false,
        audioSeek: 0,
        audioDuration: 0,
        showTime1: '00:00',
        showTime2: '00:00',
        audioTime: 0
    },
    onShow: function() {
        this.Initialization();
        this.loadaudio();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const _this = this;
        if (options.article_id) {
            _this.getArticleDetail(options.article_id);
        }
    },
    getArticleDetail: function(e) {
        const _this = this;
        util.request(getApp().api.article.detail, { id: e }).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                        article: res.data[0],
                        audiosrc: 'http://img02.tuke88.com/newpreview_music/09/00/27/5c8912c300c9227199.mp3'
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    toGoodsDetail: function() {
        wx.navigateTo({
            url: "/pages/goods-detail/goods-detail"
        })
    },
    //初始化播放器，获取duration
    Initialization() {
        var t = this;
        if (this.data.audiosrc.length != 0) {
            //设置src
            innerAudioContext.src = this.data.audiosrc;
            //运行一次
            innerAudioContext.play();
            innerAudioContext.pause();
            innerAudioContext.onCanplay(() => {
                //初始化duration
                innerAudioContext.duration
                setTimeout(function() {
                    //延时获取音频真正的duration
                    var duration = innerAudioContext.duration;
                    var min = parseInt(duration / 60);
                    var sec = parseInt(duration % 60);
                    if (min.toString().length == 1) {
                        min = `0${min}`;
                    }
                    if (sec.toString().length == 1) {
                        sec = `0${sec}`;
                    }
                    t.setData({ audioDuration: innerAudioContext.duration, showTime2: `${min}:${sec}` });
                }, 1000)
            })
        }
    },
    //拖动进度条事件
    sliderChange(e) {
        var that = this;
        innerAudioContext.src = this.data.audiosrc;
        //获取进度条百分比
        var value = e.detail.value;
        this.setData({ audioTime: value });
        var duration = this.data.audioDuration;
        //根据进度条百分比及歌曲总时间，计算拖动位置的时间
        value = parseInt(value * duration / 100);
        //更改状态
        this.setData({ audioSeek: value, isPlayAudio: true });
        //调用seek方法跳转歌曲时间
        innerAudioContext.seek(value);
        //播放歌曲
        innerAudioContext.play();
    },
    //播放、暂停按钮
    playAudio() {
        //获取播放状态和当前播放时间
        var isPlayAudio = this.data.isPlayAudio;
        var seek = this.data.audioSeek;
        innerAudioContext.pause();
        //更改播放状态
        this.setData({ isPlayAudio: !isPlayAudio })
        if (isPlayAudio) {
            //如果在播放则记录播放的时间seek，暂停
            this.setData({ audioSeek: innerAudioContext.currentTime });
        } else {
            //如果在暂停，获取播放时间并继续播放
            innerAudioContext.src = this.data.audiosrc;
            if (innerAudioContext.duration != 0) {
                this.setData({ audioDuration: innerAudioContext.duration });
            }
            //跳转到指定时间播放
            innerAudioContext.seek(seek);
            innerAudioContext.play();
        }
    },
    loadaudio() {
        var that = this;
        //设置一个计步器
        this.data.durationIntval = setInterval(function() {
            //当歌曲在播放时执行
            if (that.data.isPlayAudio == true) {
                //获取歌曲的播放时间，进度百分比
                var seek = that.data.audioSeek;
                var duration = innerAudioContext.duration;
                var time = that.data.audioTime;
                time = parseInt(100 * seek / duration);
                //当歌曲在播放时，每隔一秒歌曲播放时间+1，并计算分钟数与秒数
                var min = parseInt((seek + 1) / 60);
                var sec = parseInt(seek % 60);
                //填充字符串，使3:1这种呈现出 03：01 的样式
                if (min.toString().length == 1) {
                    min = `0${min}`;
                }
                if (sec.toString().length == 1) {
                    sec = `0${sec}`;
                }
                var min1 = parseInt(duration / 60);
                var sec1 = parseInt(duration % 60);
                if (min1.toString().length == 1) {
                    min1 = `0${min1}`;
                }
                if (sec1.toString().length == 1) {
                    sec1 = `0${sec1}`;
                }
                //当进度条完成，停止播放，并重设播放时间和进度条
                if (time >= 100) {
                    innerAudioContext.stop();
                    that.setData({ audioSeek: 0, audioTime: 0, audioDuration: duration, isPlayAudio: false, showTime1: `00:00` });
                    return false;
                }
                //正常播放，更改进度信息，更改播放时间信息
                that.setData({ audioSeek: seek + 1, audioTime: time, audioDuration: duration, showTime1: `${min}:${sec}`, showTime2: `${min1}:${sec1}` });
            }
        }, 1000);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        this.videoContext = wx.createInnerAudioContext('myVideo');
    },
    onUnload: function() {
        //卸载页面，清除计步器
        clearInterval(this.data.durationIntval);
    },
    onShareAppMessage: function(options) {
        var _this = this;
        const openid = wx.getStorageSync('openid');

        var shareObj = {
            title: '',
            path: "/pages/goods-detail/goods-detail?productId=" + _this.data.goodsId + '&openid=' + openid,
            imageUrl: 'http://hi.lengjishan.cn/new/resource/banner/banner2.jpg',
            success: function(res) {
                if (res.errMsg == 'shareAppMessage:ok') {}
            },
            fail: function() {
                if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
            },
            complete: function() {

            }
        };
        return shareObj;


    },


})